package com.erp.module.zhiLiang.xingJianZhiLiang

import android.content.Context
import android.util.Log
import com.erp.bean.ZhiLiangBean
import com.erp.contacts.Contacts
import com.erp.contacts.Contacts.BOUNDARY
import com.erp.Interface.OnUserDataInterface
import com.erp.bean.PhotoBean
import com.erp.util.Httputil
import com.erp.util.Httputil.addtoken
import org.xutils.http.RequestParams
import org.xutils.x
import java.io.File

/**
 * Created by Administrator on 2019/3/20/020.
 * 作者：刘洋
 */
class XJWuLiaoZhiLiangModel {

    /**获取物料质量代码 */
    fun getZhiLiangDaiMa(context: Context, onUserDataInterface: OnUserDataInterface) {
        val params = RequestParams(Contacts.ZHILIAN_DAIMA)
        Httputil.getparams(context, params)
        x.http().get<String>(params, Httputil.Call(context, onUserDataInterface))
    }
    /**获取工号别表 */
    fun getZhiLiangGongHaoLieBiao(context: Context, onUserDataInterface: OnUserDataInterface) {
        val params = RequestParams(Contacts.ZHILIAN_GONGHAO)
        Log.d("TAAAG", "params=$params")
        Httputil.getparams(context, params)
        x.http().get<String>(params, Httputil.Call(context, onUserDataInterface))
    }

    /**获取辅助数据 */
    fun getmaterial_quality_params(context: Context, onUserDataInterface: OnUserDataInterface) {
        val params = RequestParams(Contacts.ZHILIANG+"/configuration/material_quality_params")
        Httputil.getparams(context, params)
        x.http().get<String>(params, Httputil.Call(context, onUserDataInterface))
    }

    /**修改物料质量问题 */
    fun setZhiLiang(context: Context, code: Int, bean: ZhiLiangBean, deletephoto: List<PhotoBean>, onUserDataInterface: OnUserDataInterface) {
        var params: RequestParams? = null
        if (code == 1) {
            params = RequestParams(Contacts.XIUGAI_ZHILIANG)
        } else if (code == 2) {
            params = RequestParams(Contacts.XIUGAI_ZHILIANG + "/" + bean.id + "/edit")
        } else if (code == 3) {
            params = RequestParams(Contacts.XIUGAI_ZHILIANG + "&multiple=1")
        }
        Httputil.getparams(context, params)
        addtoken(params!!)
        params!!.addHeader("ACCEPT", "application/json")
        params.addBodyParameter("mcode", bean.mcode, BOUNDARY)
        params.addBodyParameter("count", bean.count, BOUNDARY)
        params.addBodyParameter("qi_id", bean.qi_id, BOUNDARY)
        params.addBodyParameter("work_number", bean.work_number, BOUNDARY)
        params.addBodyParameter("occurrence_time", bean.occurrence_time, BOUNDARY)
        params.addBodyParameter("description", bean.description, BOUNDARY)
        params.addBodyParameter("handle_way", bean.handle_way, BOUNDARY)
        params.addBodyParameter("dnum", bean.dnum, BOUNDARY)
        params.addBodyParameter("solution", bean.solution, BOUNDARY)
        params.addBodyParameter("pname",bean.pname)
        for (i in bean.listphoto.indices) {
            val photo = bean.listphoto[i]
            if (photo.fid == "") {
                params.addBodyParameter("files[new][$i]", File(photo.uri), BOUNDARY)
            }
        }
        for (i in deletephoto.indices) {
            if (deletephoto[i].fid != "") {
                params.addBodyParameter("files[delete][" + deletephoto[i].fid + "]", deletephoto[i].fid, BOUNDARY)
            }
        }
        x.http().post<String>(params, Httputil.Call(context, onUserDataInterface))
    }

    /**确认收货 */
    fun querenShouhuo(context: Context, mq_id : String, onUserDataInterface: OnUserDataInterface) {
        var params = RequestParams(Contacts.XIUGAI_ZHILIANG + "/material_quality_consignee")
        Httputil.getparams(context, params)
        addtoken(params!!)
        params!!.addHeader("ACCEPT", "application/json")
        params.addBodyParameter("mq_id", mq_id, BOUNDARY)
        params.addBodyParameter("", "", BOUNDARY)
        x.http().post<String>(params, Httputil.Call(context, onUserDataInterface))
    }
}